package systemActions;

import questionsObjects.*;
import usersObjects.*;
import systemMethods.*;

/**
 * Created with IntelliJ IDEA.
 * User: xpk
 * Date: 11/12/13
 * Time: 4:23 PM
 * To change this template use File | Settings | File Templates.
 */
public class PreparationOfQuestions extends SystemActionsDecorator {
    private SystemAction currentSystemAction;

    public PreparationOfQuestions(SystemAction currentSystemAction) {
        this.currentSystemAction = currentSystemAction;
    }

    @Override
    public void executeAction(StorageOfUsers currentStorageOfUser, UserCertificate currentUserCertificate) {
        String[] massOfQuestionsAndAnswers;
        Questions storageOfQuestions = new Questions();

        massOfQuestionsAndAnswers = TestingSystemUtils.doXSLT("question.xsl", "questions.xml").split(String.format("%n"));
        createQuestions(storageOfQuestions, massOfQuestionsAndAnswers);
        currentStorageOfUser.setQuestionsForUser(storageOfQuestions, currentUserCertificate);

        currentSystemAction.executeAction(currentStorageOfUser, currentUserCertificate);
    }

    private void createQuestions(Questions storageOfQuestions, String[] massOfQuestionsAndAnswers) {
        FeaturesOfQuestion featuresOfQuestion;
        UniversalQuestion universalQuestion;

        featuresOfQuestion = createFeaturesOfQuestion("");

        for (String s : massOfQuestionsAndAnswers) {
            String[] strings = s.split("[;]");
            if (strings.length == 1) {
                featuresOfQuestion = createFeaturesOfQuestion(strings[0]);
            } else {
                universalQuestion = new UniversalQuestion(strings[0], strings[1],
                        strings[2], featuresOfQuestion);
                storageOfQuestions.add(universalQuestion);
            }
        }
    }

    private FeaturesOfQuestion createFeaturesOfQuestion(String splittedString) {
        String themeOfQuestion = "other";
        int complexityOfQuestion = 1;
        FeaturesOfQuestion featuresOfQuestion = new FeaturesOfQuestion(themeOfQuestion, complexityOfQuestion);

        String[] strings = splittedString.split("-");
        if (strings.length == 2) {
            themeOfQuestion = strings[0];
            complexityOfQuestion = Integer.parseInt(strings[1]);
            featuresOfQuestion = new FeaturesOfQuestion(themeOfQuestion, complexityOfQuestion);
        }

        return featuresOfQuestion;
    }
}
